package login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf8");
        //读取请求参数判断是否正确
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        if(username==null||username==""||password==null||password==""){
            resp.getWriter().write("用户名或者密码不完整! 登录失败!");
            return;
        }
        // 验证用户名密码是否正确
        if(!username.equals("admin")||!password.equals("123456")){
            resp.getWriter().write("用户名或者密码错误! 登录失败!");
            return;
        }
        HttpSession session= req.getSession(true);//如果没有session就创建一个
        session.setAttribute("username","admin");
        if (session.getAttribute("visitCount")==null){
            session.setAttribute("visitCount",0);
        }
        resp.sendRedirect("index");//重定向跳转
    }
}
